recursive algorithms

自己呼叫自己,注意別進入無限呼叫

例 : n階層

n! = n * (n-1)! => fact(n) = n * fact(n-1)

0!=1                    //Boundary condition

Code :

int fact(int n)
{
    if(n==0)            //Boundary condition
        return 1;
    else
        return(n*fact(n-1));
}

Boundary condition 邊界條件,何時必須停止!

results matching ""

    No results matching ""